﻿/*
    Copyright (c) 2012, Matías López
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted 
    provided that the following conditions are met:
        *   Redistributions of source code must retain the above copyright notice, this list of 
            conditions and the following disclaimer.
        *   Redistributions in binary form must reproduce the above copyright notice, this list of 
            conditions and the following disclaimer in the documentation and/or other materials 
            provided with the distribution.
        *   Neither the name of Matías López nor the names of its contributors may be used to 
            endorse or promote products derived from this software without specific prior written 
            permission.
    
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
    POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;

namespace mana.Data.Azure.Tables
{
    /// <summary>
    /// Defines Tables operations to apply to the Azure Table Storage.
    /// </summary>
    public class TableManager
    {
        private CloudTableClient _tableClient;

        /// <summary>
        /// Initializes an instance of the TableManager with the targeted Cloud Storage Account.
        /// </summary>
        /// <param name="account"></param>
        public TableManager(CloudStorageAccount account)
        {
            _tableClient = account.CreateCloudTableClient();
        }

        #region Properties

        /// <summary>
        /// Azure Table Client.
        /// </summary>
        public CloudTableClient TableClient
        {
            get { return _tableClient; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Adds an Azure Table with the specified name (if not exists).
        /// </summary>
        /// <param name="tableName">Table name to add.</param>
        /// <returns>Returns a boolean indicating whether the Azure Table was added or not.</returns>
        public bool Add(string tableName)
        {
            return TableClient.CreateTableIfNotExist(tableName);
        }

        /// <summary>
        /// Deletes the specified Azure Table (if exists).
        /// </summary>
        /// <param name="tableName">Table name to delete.</param>
        /// <returns>Returns a boolean indicating whether the Azure Table was added or not.</returns>
        public bool Delete(string tableName)
        {
            return TableClient.DeleteTableIfExist(tableName);
        }

        /// <summary>
        /// Indicates whether the specified Azure Table exists.
        /// </summary>
        /// <param name="tableName">Table name to check.</param>
        /// <returns>Returns a boolean indicating whether the Azure Table exists or not.</returns>
        public bool Exists(string tableName)
        {
            return TableClient.DoesTableExist(tableName);
        }

        /// <summary>
        /// Gets all the Azure Tables names in the Azure Table Storage.
        /// </summary>
        /// <returns>Returns the existing Azure Tables names.</returns>
        public IEnumerable<string> GetNames()
        {
            return TableClient.ListTables();
        }

        /// <summary>
        /// Gets all the Azure Tables names in the Azure Table Storage that starts with the specified name prefix.
        /// </summary>
        /// <param name="tableNamePrefix"></param>
        /// <returns>Returns the existing Azure Tables names that starts with the specified name prefix.</returns>
        public IEnumerable<string> GetNames(string tableNamePrefix)
        {
            return TableClient.ListTables(tableNamePrefix);
        }

        #endregion Methods
    }
}
